package com.jeff.springWebTestNG.aspect;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import com.jeff.springWebTestNG.service.exception.Exception1;

@Component
@Aspect
public class BasicServiceAspect implements Ordered {
	@Pointcut("execution(public * *(..))")
    public void anyPublicOperation() {}
	
	@Pointcut("execution(* com.jeff.springWebTestNG.service.IServiceA.*(..))")
    public void anyServiceAOperation() {}
	
	@Pointcut("execution(* com.jeff.springWebTestNG.service.IServiceB.*(..))")
    public void anyServiceBOperation() {}
	
	
//	@Before("anyServiceAOperation() || anyServiceBOperation()")
//	@Before("anyPublicOperation() && target(com.jeff.springWebTestNG.service.ITarget)")
	@Before("anyPublicOperation() && @target(com.jeff.springWebTestNG.TracingService)")
	public void doServiceBeforeCheck() {
		System.out.println("Service Access Checked!");
	}
	
	@Pointcut("execution(* com.jeff.springWebTestNG.service.IServiceA.hasReturnMethod1(..))")
    public void serviceAReturn1() {}
	
	
	@After("serviceAReturn1()")
	public void printReturn1() {
		System.out.println("After Return1!");
	}
	
	@AfterReturning(pointcut="serviceAReturn1()", returning="retVal")
	public void printReturn1RetVal(Object retVal) {
		System.out.println(retVal);
	}
	
	@Pointcut("execution(* com.jeff.springWebTestNG.service.IServiceA.throwExceptions(..))")
	public void serviceAThrowExceptions() {}
	
	@AfterThrowing(pointcut="serviceAThrowExceptions()", throwing="e")
	public void printExceptions(Exception1 e) {
		System.out.println("Aspect print: " + e.getMessage());
	}
	
	
	@Override
	public int getOrder() {
		// TODO Auto-generated method stub
		return 1;
	}
	
}
